/**
 * 
 */
package com.tomagency.cyberlab.client.ui.renderer;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.tomagency.cyberlab.client.ClientFactory;
import com.tomagency.cyberlab.client.events.MessageEvent;
import com.tomagency.cyberlab.client.ui.ObjectListenView.CellRenderer;
import com.tomagency.cyberlab.client.ui.ObjectListenView.ObjectEventHandler;
import com.tomagency.cyberlab.client.ui.ObjectListenView.RenderType;
import com.tomagency.cyberlab.client.ui.ObjectListenView.RowRenderer;
import com.tomagency.cyberlab.client.ui.ObjectListenView.RowType;
import de.gammadata.gwt.proxy.MessageListProxy;

/**
 * @author gfr
 *
 */
public class MessageRowRendererImpl implements RowRenderer<MessageEvent> {
	

	
	List<CellRenderer<MessageEvent>> cellRendererList;

	public MessageRowRendererImpl() {
		bind();
	}
	
	private void bind(){
		cellRendererList = new ArrayList<CellRenderer<MessageEvent>>();
		cellRendererList.add(new DatumRenderer());
		cellRendererList.add(new SubjectRenderer());
	}

	/**
	 * @see com.tomagency.cyberlab.client.ui.ObjectListenView.RowRenderer#getCellRendererList(com.tomagency.cyberlab.client.ui.ObjectListenView.RenderType)
	 */
	@Override
	public List<CellRenderer<MessageEvent>> getCellRendererList(RenderType renderType) {
		return cellRendererList;
	}
	
	class DatumRenderer implements CellRenderer<MessageEvent>{

		@Override
		public int getColSpan() {
			return 1;
		}

		@Override
		public Widget getColumnWidget(RowType rowType, ObjectEventHandler handler,
				Object dataObj) {
			MessageListProxy message = (MessageListProxy)dataObj;
			Label date = new Label(ClientFactory.dateTimeFormat.format(message.getDate()));
			return date;
		}
		
	}
	
	class SubjectRenderer implements CellRenderer<MessageEvent>{

		@Override
		public int getColSpan() {
			return 1;
		}

		@Override
		public Widget getColumnWidget(RowType rowType, ObjectEventHandler handler,
				Object dataObj) {
			MessageListProxy message = (MessageListProxy)dataObj;
			Label label = new Label();
			if (message.getSubject()!=null&&message.getSubject().length()>ClientFactory.MAX_SUBJECT_LENGTH)
				label.setText(message.getSubject().substring(0, ClientFactory.MAX_SUBJECT_LENGTH)+ClientFactory.MORE_STRING);
			else
				label.setText(message.getSubject());
			return label;
		}
		
	}

}
